const fs = require('fs')

const service = require('../service/user_service')
const fileService = require('../service/file_service')
const {AVATAR_PATH} = require('../constants/file_path')

class UserController{
  // 注册
  async create(ctx,next){
    // 获取用户请求参数
    const user = ctx.request.body
    // 查询数据库
    const result = await service.create(user)
    // 返回数据
    ctx.body = result
  }

  async avatarInfo(ctx,next){
    const {userId} = ctx.params
    const result = await fileService.getAvatarByUserId(userId)
    // 提供图像信息
    ctx.response.set('content-type', result.mimetype) // 没有这一行浏览器默认当文件下载处理
    ctx.body = fs.createReadStream(`${AVATAR_PATH}/${result.filename}`)
  }
}

module.exports = new UserController()